package com.rzgeo.rzmaplib.action;

import android.app.Activity;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.esri.arcgisruntime.data.Feature;
import com.esri.arcgisruntime.data.FeatureTable;
import com.esri.arcgisruntime.geometry.Geometry;
import com.esri.arcgisruntime.geometry.Multipart;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.Polygon;
import com.esri.arcgisruntime.geometry.Polyline;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.mapping.view.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.mapping.view.SketchCreationMode;
import com.esri.arcgisruntime.mapping.view.SketchEditor;
import com.esri.arcgisruntime.mapping.view.SketchGeometryChangedEvent;
import com.esri.arcgisruntime.mapping.view.SketchGeometryChangedListener;
import com.rzgeo.rzmaplib.R;
import com.rzgeo.rzmaplib.util.RmlUtil;

import java.util.ArrayList;

/**
 * Created by zhanghang on 2018/1/27.
 */

public class RmlMarkPolygonDrawAction implements ActionMode.Callback, SketchGeometryChangedListener {
    private final MapView _mapView;
    private final SketchEditor _ske;
    private final Activity _activity;
    private final FeatureLayer _targetLayer;
    private String _msg;
    private SketchEditor _oldSKE;
    private ActionMode _mode;


    public RmlMarkPolygonDrawAction(Activity activity, MapView mapView, FeatureLayer targetLayer) {
        _activity=activity;
        _mapView = mapView;
        _targetLayer=targetLayer;
        _ske = new SketchEditor();
        _ske.addGeometryChangedListener(this);
        _msg=null;
    }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        _mode=mode;
        MenuInflater inflater = _activity.getMenuInflater();
        inflater.inflate(R.menu.rml_mark_draw, menu);
        return true;
    }

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        _oldSKE = _mapView.getSketchEditor();
        _mapView.setSketchEditor(_ske);
        _ske.start(SketchCreationMode.POLYGON);
        mode.setTitle("请在地图上点击绘制多边形");
        return false;
    }

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        if (item.getItemId() == R.id.act_undo) {
            _ske.undo();
        }else if (item.getItemId() == R.id.act_redo) {
            _ske.redo();
        }
        else if (item.getItemId() == R.id.act_clear) {
            _ske.clearGeometry();
        }
        else if (item.getItemId() == R.id.act_set_attr) {
            setMsg();
        }
        else if (item.getItemId() == R.id.act_cancel) {
            _mode.finish();
        }
        else if (item.getItemId() == R.id.act_ok) {
            return finishDraw();
        }
        else {
            return false;
        }
        return true;
    }

    protected boolean finishDraw(){
        if(!_ske.isSketchValid())
        {
            RmlUtil.Tip.Toast("没有数据需要保存");
            _mode.finish();
            return true;
        }
        if(_msg==null)
        {
            RmlUtil.Tip.Toast("没有设置注释");
            setMsg();
            return true;
        }
        if(_targetLayer==null)
        {
            RmlUtil.Tip.Toast("没有图层");
            _mode.finish();
            return true;
        }
        if(!_targetLayer.getFeatureTable().isEditable())
        {
            RmlUtil.Tip.Toast("图层不可编辑");
            _mode.finish();
            return true;
        }


        final FeatureTable ft = _targetLayer.getFeatureTable();

        Feature f = ft.createFeature();
        f.setGeometry(_ske.getGeometry());
        f.getAttributes().put("Title",_msg);
        ft.addFeatureAsync(f);

        ft.addDoneLoadingListener(new Runnable() {
            @Override
            public void run() {
                RmlUtil.Tip.Toast("添加完成");
                ft.removeDoneLoadingListener(this);
            }
        });
        _mode.finish();
        return true;
    }

    private void setMsg() {

        RmlUtil.Dialog.InputString(_activity, "", new RmlUtil.Dialog.OnInputText() {
            @Override
            public void onResult(String content) {
                _msg=content;
                finishDraw();
            }

            @Override
            public void onCancel() {

            }
        });
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
        _mapView.setSketchEditor(_oldSKE);
    }

    @Override
    public void geometryChanged(SketchGeometryChangedEvent e) {


    }
}
